package com.team.mp.api.service.impl;


import javax.annotation.Resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.team.mp.api.bo.HealthRecordAddBo;
import com.team.mp.api.service.intf.HealthrecordService;
import com.team.mp.mapper.dao.HealthrecordDao;
import com.team.mp.mapper.entity.HealthRecord;
import com.team.mp.vo.R;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * @program: MyProject
 * @description: 业务接口实现层
 * @author: yyj
 * @create: 2023-03-28 20:40:12
 */
@Service
public class HealthrecordServiceImpl implements HealthrecordService {
    /**
     * 注入持久层对象
     */
    @Resource
    private HealthrecordDao dao;

    /**
     * 新增内容
     */
    @Override
    public R save(HealthRecordAddBo bo) {
        if (bo != null) {
            HealthRecord healthrecord = new HealthRecord();
            BeanUtils.copyProperties(bo, healthrecord);
            int i = dao.insert(healthrecord);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，新增异常！");
    }

    /**
     * 修改内容
     */
    @Override
    public R update(HealthRecord healthrecord) {
        if (healthrecord != null) {
            int i = dao.updateById(healthrecord);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("亲，修改异常！");
    }

    /**
     * 删除
     */
    @Override
    public R del(long id) {
        if (id > 0) {
            int i = dao.deleteById(id);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("删除异常");
    }

    /**
     * 根据类型和餐前餐后进行查询
     */
    @Override
    public R queryByCond(Integer uid, Integer type, Integer mealornot, int page, int size) {
        QueryWrapper<HealthRecord> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type)
                .eq("mealornot", mealornot)
                .eq("uid",uid);
        Page<HealthRecord> recordPage = dao.selectPage(new Page<>(page, size), wrapper);
        return R.ok(recordPage);
    }

    /**
     * 查询 所有数据
     */
    @Override
    public R queryAll() {
        return R.ok(dao.selectList(null));
    }
}
